package yaohua.entity;

import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.nodes.Document;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import yaohua.client.MyClient;
import yaohua.jsoup.Jsoup_help;

public class Entity_help
	{
		private HttpClient httpclient;

		/**
		 * 通过HttpPost方法且两对参数过得HttpResponse
		 * 
		 * @param url
		 * @param param_name0
		 * @param param_value0
		 * @param param_name1
		 * @param param_value1
		 * @return
		 */
		public HttpResponse postResponse(String url, String param_name0, String param_value0, String param_name1, String param_value1)
			{
				httpclient = MyClient.getClient();
				HttpPost httppost = new HttpPost(url);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair(param_name0, param_value0));
				params.add(new BasicNameValuePair(param_name1, param_value1));
				HttpResponse httpResponse = null;
				try
					{
						httppost.setEntity(new UrlEncodedFormEntity(params, "gb2312"));
						httpResponse = httpclient.execute(httppost);

					}
				catch (UnsupportedEncodingException e)
					{
						e.printStackTrace();
					}
				catch (ClientProtocolException e)
					{
						e.printStackTrace();
					}
				catch (HttpHostConnectException e)
					{
						e.printStackTrace();
					}
				catch (IOException e)
					{
						e.printStackTrace();
					}
				catch (Exception e)
					{
						e.printStackTrace();
					}

				return httpResponse;

			}

		/**
		 * 通过HttpPost且只有一个参数对获得HttpResponse
		 * 
		 * @param url
		 * @param param_name
		 * @param param_value
		 * @return
		 */
		public HttpResponse postResponse(String url, String param_name, String param_value)
			{
				httpclient = MyClient.getClient();
				HttpPost httppost = new HttpPost(url);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair(param_name, param_value));
				HttpResponse httpResponse = null;
				try
					{
						httppost.setEntity(new UrlEncodedFormEntity(params, "gb2312"));
						httpResponse = httpclient.execute(httppost);
					}
				catch (UnsupportedEncodingException e)
					{
						e.printStackTrace();
					}
				catch (ClientProtocolException e)
					{
						e.printStackTrace();
					}
				catch (HttpHostConnectException e)
					{
						e.printStackTrace();
					}
				catch (IOException e)
					{
						e.printStackTrace();
					}

				return httpResponse;

			}

		/**
		 * 重载参数为8个
		 * 
		 * @param url
		 * @param param_name1
		 * @param param_value1
		 * @param param_name2
		 * @param param_value2
		 * @param param_name3
		 * @param param_value3
		 * @param param_name4
		 * @param param_value4
		 * @param param_name5
		 * @param param_value5
		 * @param param_name6
		 * @param param_value6
		 * @param param_name7
		 * @param param_value7
		 * @param param_name8
		 * @param param_value8
		 * @return
		 */

		public HttpResponse postResponse(String url, String param_name1, String param_value1, String param_name2, String param_value2,
				String param_name3, String param_value3, String param_name4, String param_value4, String param_name5,
				String param_value5, String param_name6, String param_value6, String param_name7, String param_value7,
				String param_name8, String param_value8)
			{
				httpclient = MyClient.getClient();
				HttpPost httppost = new HttpPost(url);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair(param_name1, param_value1));
				params.add(new BasicNameValuePair(param_name2, param_value2));
				params.add(new BasicNameValuePair(param_name3, param_value3));
				params.add(new BasicNameValuePair(param_name4, param_value4));
				params.add(new BasicNameValuePair(param_name5, param_value5));
				params.add(new BasicNameValuePair(param_name6, param_value6));
				params.add(new BasicNameValuePair(param_name7, param_value7));
				params.add(new BasicNameValuePair(param_name8, param_value8));
				HttpResponse httpResponse = null;
				try
					{
						httppost.setEntity(new UrlEncodedFormEntity(params, "gb2312"));
						httpResponse = httpclient.execute(httppost);
					}
				catch (UnsupportedEncodingException e)
					{
						e.printStackTrace();
					}
				catch (ClientProtocolException e)
					{
						e.printStackTrace();
					}
				catch (HttpHostConnectException e)
					{
						e.printStackTrace();
					}
				catch (IOException e)
					{
						e.printStackTrace();
					}

				return httpResponse;
			}

		/**
		 * 通过HttpGet方法得到HttpResponse
		 * 
		 * @param url
		 * @return
		 */
		public HttpResponse getResponse(String url)
			{
				HttpGet httpget = new HttpGet(url);
				httpclient = MyClient.getClient();
				HttpResponse httpResponse = null;
				try
					{
						httpResponse = httpclient.execute(httpget);
					}
				catch (ClientProtocolException e)
					{
						e.printStackTrace();
					}
				catch (HttpHostConnectException e)
					{
						e.printStackTrace();
					}
				catch (IOException e)
					{
						e.printStackTrace();
					}

				return httpResponse;
			}

		/**
		 * 返回网页源数据
		 * 
		 * @param httpentity
		 * @return
		 */
		public String getContent(HttpEntity httpentity)
			{
				String result = null;
				try
					{
						result = EntityUtils.toString(httpentity, "gb2312");
					}
				catch (ParseException e)
					{
						e.printStackTrace();
					}
				catch (IllegalArgumentException e)
					{
						e.printStackTrace();
					}
				catch (IOException e)
					{
						e.printStackTrace();
					}
				return result;
			}

		/**
		 * 通过HttpResponse获得HttpEntity
		 * 
		 * @param httpResponse
		 * @return
		 */
		public HttpEntity getEntity(HttpResponse httpResponse)
			{

				HttpEntity httpEntity = null;
				try
					{
						httpEntity = httpResponse.getEntity();
					}
				catch (Exception e)
					{
						e.printStackTrace();
					}
				return httpEntity;
			}

		/**
		 * 验证httpResponse的返回码是否为200
		 * 
		 * @param httpResponse
		 * @return
		 */
		public boolean code200(HttpResponse httpResponse)
			{
				int code = 0;
				try
					{
						code = httpResponse.getStatusLine().getStatusCode();
					}
				catch (Exception e)
					{

						e.printStackTrace();
					}
				if (code == 200)
					{
						return true;
					}
				else
					{
						return false;
					}
			}

		/**
		 * 处理搜索数目列表时用
		 * 主要在使用html中 class来解析得到数据用的
		 * 
		 * @param httpResponse
		 * @param handler
		 * @param label
		 * @param start
		 * @param add
		 * @param progressDialog
		 */

		public void handleBook(HttpResponse httpResponse, Handler handler, String label, int startBook, int addBook,int startAuthor,int addAuthor,
				ProgressDialog progressDialog)
			{
				// 如果网络中断，此处httpResponse==null;
				if (httpResponse == null)
					{
						Message message = new Message();
						message.arg1 = -1;
						handler.sendMessage(message);
					}

				HttpEntity entity = getEntity(httpResponse);
				String result = getContent(entity);
				// 测试Jsoup的方法
				Jsoup_help jsoup_help = new Jsoup_help();
				Document doc = jsoup_help.getDocument(result);

				ArrayList<String> list = (ArrayList<String>) jsoup_help.geTextListByClass(doc, label);

				// 把书籍名称放入List
				ArrayList<String> books = new ArrayList<String>();
				for (int i = startBook; i < list.size(); i = i + addBook)
					{
						books.add(list.get(i));

					}
				//将作者放入List
				ArrayList<String> authors=new ArrayList<String>();
				for(int i=startAuthor;i<list.size();i=i+addAuthor)
					{
						authors.add(list.get(i));
					}
				// 创建带有Map的List，用于传送给ItemActivity
				List<Map<String, String>> newBooks = new ArrayList<Map<String, String>>();
				// 向listItem中装入map
				for (int i = 0;i<books.size(); i++)
					{
						Map<String, String> map = new HashMap<String, String>();
						map.put("title", books.get(i));
						map.put("value", authors.get(i));
						newBooks.add(map);
					}
				ArrayList<String> storeUrl = new Jsoup_help().getStoreUrl(result);
				// 发送message,传送数据
				Message msg = new Message();
				Bundle bundle = new Bundle();
				bundle.putString("result", result);
				bundle.putStringArrayList("list", list);
				bundle.putStringArrayList("books", books);
				bundle.putSerializable("newBooks", (Serializable) newBooks);
				bundle.putStringArrayList("storeUrl", storeUrl);
				msg.setData(bundle);
				// msg.obj = books;
				handler.sendMessage(msg);
				// progressDialog.dismiss();
			}
		/**
		 * 处理馆藏时候用
         * 主要在使用html中 class来解析得到数据用的
         *
         * @param httpResponse
         * @param handler
         * @param label
         * @param start
         * @param add
         * @param progressDialog
         */

        public void handleBook(HttpResponse httpResponse, Handler handler, String label, 
                        ProgressDialog progressDialog)
                {
                        // 如果网络中断，此处httpResponse==null;
                        if (httpResponse == null)
                                {
                                        Message message = new Message();
                                        message.arg1 = -1;
                                        handler.sendMessage(message);
                                }

                        HttpEntity entity = getEntity(httpResponse);
                        String result = getContent(entity);
                        // 测试Jsoup的方法
                        Jsoup_help jsoup_help = new Jsoup_help();
                        Document doc = jsoup_help.getDocument(result);

                        ArrayList<String> list = (ArrayList<String>) jsoup_help.geTextListByClass(doc, label);

                      
                        // 发送message,传送数据
                        Message msg = new Message();
                        Bundle bundle = new Bundle();
                        bundle.putString("result", result);
                        bundle.putStringArrayList("list", list);
                       // bundle.putStringArrayList("books", books);
                       // bundle.putStringArrayList("storeUrl", storeUrl);
                        msg.setData(bundle);
                        // msg.obj = books;
                        handler.sendMessage(msg);
                        // progressDialog.dismiss();
                }
        /**
         * 借还查询，过期查询，预约查询中使用
         * @param httpResponse
         * @param handler
         * @param label
         * @param start
         * @param add
         * @param progressDialog
         */

        public void handleBook(HttpResponse httpResponse, Handler handler, String label, int start, int add,
                ProgressDialog progressDialog)
        {
                // 如果网络中断，此处httpResponse==null;
                if (httpResponse == null)
                        {
                                Message message = new Message();
                                message.arg1 = -1;
                                handler.sendMessage(message);
                        }

                HttpEntity entity = getEntity(httpResponse);
                String result = getContent(entity);
                // 测试Jsoup的方法
                Jsoup_help jsoup_help = new Jsoup_help();
                Document doc = jsoup_help.getDocument(result);
                //获得所有信息的list
                ArrayList<String> list = (ArrayList<String>) jsoup_help.geTextListByClass(doc, label);
                //获得续借的URL list
                ArrayList<String> renewUrl=new Jsoup_help().getRenewUrl(result);
               
                // 把书籍名称放入List
                ArrayList<String> books = new ArrayList<String>();
                for (int i = start; i < list.size(); i = i + add)
                        {
                                books.add(list.get(i));

                        }
       
               
                // 发送message,传送数据
                Message msg = new Message();
                Bundle bundle = new Bundle();
                bundle.putString("result", result);
                bundle.putStringArrayList("list", list);
                bundle.putStringArrayList("books", books);
                bundle.putStringArrayList("renewUrl", renewUrl);
                msg.setData(bundle);
                handler.sendMessage(msg);
           
        }


	}

